import json
import time

import numpy as np

from ScrapyObject.spiders.utils.aa import read_json_file, spiderInfo, VideoBean


class RoomListBean:
    timeStamp = 0
    data = []

    def __init__(self, timeStamp, data):
        self.timeStamp = timeStamp
        self.data = data


class RoomListItemBean:
    videoTag = ""
    videoUrl = ""

    def __init__(self, videoTag, videoUrl):
        self.videoTag = videoTag
        self.videoUrl = videoUrl


if __name__ == '__main__':
    """
    3把最终结果分解成几份
    """
    print('开始')
    timestamp = int(time.time())
    jsonStr = ""
    # jsonStr = read_json_file('D:\\' + spiderInfo.name + '.json')
    jsonStr = read_json_file('/home/ccg/3/' + spiderInfo.name + '.json')
    print("原始数据一共: " + str(len(jsonStr)))
    large_array = []
    for i in jsonStr:
        video = VideoBean()
        video.id = 0
        video.name = i['name']
        video.url = i['url']
        video.tags = i['tags']
        video.pUrl = i['pUrl']
        video.vUrl = i['vUrl']
        large_array.append(video)
    jsonStr.clear()
    # 定义每组的大小
    chunk_size = 100_000

    # 计算需要分割成多少组
    num_chunks = len(large_array) // chunk_size
    if len(large_array) % chunk_size != 0:
        num_chunks += 1  # 如果不能整除，需要额外一个 chunk

    # 使用 numpy.array_split 进行分割
    chunks = np.array_split(large_array, num_chunks)

    # chunks 现在是一个包含多个子数组的列表
    print(f"总共分割成 {len(chunks)} 个数组:")
    videoFinalList = []
    for i, chunk in enumerate(chunks):
        videoFinalList.clear()
        for j in chunk:
            tinydict = {'id': 0, 'name': j.name, 'url': '', 'tags': j.tags, 'pUrl': j.pUrl, 'vUrl': j.vUrl}
            videoFinalList.append(tinydict)
        # 将JSON数组转换为字符串
        json_string = json.dumps(videoFinalList, ensure_ascii=False)
        # 将字符串写入txt文件
        # with open('D:\\' + spiderInfo.name + "" + str(i) + '.json', 'w', encoding='utf-8') as file:
        with open('/home/ccg/3/' + spiderInfo.name + "" + str(i) + '.json', 'w', encoding='utf-8') as file:
            file.write(json_string)
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
